package com.bos.editor;

import com.bos.dao.TriwulanDAO;
import com.bos.domain.Triwulan;

import java.beans.PropertyEditorSupport;

/**
 * User: Deny Prasetyo, S.T.
 * http://www.jasoet.com
 */
public class TriwulanEditor extends PropertyEditorSupport {

    private TriwulanDAO triwulanDAO;

    public TriwulanEditor(TriwulanDAO triwulanDAO) {
        this.triwulanDAO = triwulanDAO;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            long id = Long.parseLong(text);

            Triwulan result = triwulanDAO.get(id);

            if (result != null) {
                setValue(result);
            } else {
                throw new IllegalArgumentException("Triwulan with id " + id + " not Found!");
            }

        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(ex.getMessage());
        }
    }

    @Override
    public String getAsText() {
        if (getValue() != null) {
            Triwulan result = (Triwulan) getValue();
            return result.getId() + "";
        } else {
            return "";
        }
    }
}
